<template>
    <div>
    </div>
</template>

<script setup lang="ts">
import { useVueCesium } from 'vue-cesium'
import type { VcViewerProvider, VcReadyObject } from 'vue-cesium/es/utils/types'
import { dynamicLine } from './dynamicLine'

const $vc: VcViewerProvider = useVueCesium()
$vc.creatingPromise.then((readyObj: VcReadyObject) => {
    let Cesium: any = readyObj.Cesium
    let viewer = readyObj.viewer

    let urlPath = new URL('/tileset/tileset.json', import.meta.url).href
    let urlPath2 = new URL('/road/roads.json', import.meta.url).href


    dynamicLine(Cesium)
    Cesium.GeoJsonDataSource.load(urlPath2).then(function (dataSource) {
        console.log(dataSource);

        viewer.dataSources.add(dataSource);
        const entities = dataSource.entities.values;
        for (let i = 0; i < entities.length; i++) {
            let entity = entities[i];
            entity.polyline.width = 1.7;
            entity.polyline.material = new Cesium.Spriteline1MaterialProperty(3000, new URL('./imgs/color5.png', import.meta.url).href);
        }
    });

    const tileset = viewer.scene.primitives.add(
        new Cesium.Cesium3DTileset({
            url: urlPath,
            dynamicScreenSpaceError: true,
            cullWithChildrenBounds: false,
            skipLevels: 0,
            maximumScreenSpaceError: 0,
        })
    );

    // 加载3DTitle，并设置渐变光环
    tileset.tileVisible.addEventListener(function (tile) {
        let content = tile.content;
        let featuresLength = content.featuresLength;
        let feature;
        for (var i = 0; i < featuresLength; i += 2) {
            feature = content.getFeature(i);
            let _model = feature.content._model;
            if (_model && _model._pipelineResources) {
                let program = _model._pipelineResources[1];
                const color = `vec4(0.0, 0.3, 0.8, 0.8)`;
                program._fragmentShaderSource.sources[0] = `
							uniform vec2 model_iblFactor;
							uniform mat3 model_iblReferenceFrameMatrix;
							uniform float model_luminanceAtZenith;
							uniform float u_metallicFactor;
							uniform float u_roughnessFactor;
							uniform int model_featuresLength;
							uniform sampler2D model_batchTexture;
							uniform vec4 model_textureStep;
							uniform float model_colorBlend;
							uniform bool model_commandTranslucent;
							uniform sampler2D model_pickTexture;
							varying vec3 v_positionWC;
							varying vec3 v_positionEC;
							varying vec3 v_normalEC;
							varying vec3 v_positionMC;
							varying float v_featureId_0;
							struct SelectedFeature
							{
								int id;
								vec2 st;
								vec4 color;
							};
							struct FeatureIds
							{
								int featureId_0;
							};
							vec2 computeSt(float featureId)
							{
								float stepX = model_textureStep.x;
								float centerX = model_textureStep.y;

								#ifdef MULTILINE_BATCH_TEXTURE
								float stepY = model_textureStep.z;
								float centerY = model_textureStep.w;

								float xId = mod(featureId, model_textureDimensions.x); 
								float yId = floor(featureId / model_textureDimensions.x);
								
								return vec2(centerX + (xId * stepX), centerY + (yId * stepY));
								#else
								return vec2(centerX + (featureId * stepX), 0.5);
								#endif
							}
							void selectedFeatureIdStage(out SelectedFeature feature, FeatureIds featureIds)
							{   
								int featureId = featureIds.SELECTED_FEATURE_ID;
								if (featureId < model_featuresLength)
								{
									vec2 featureSt = computeSt(float(featureId));

									feature.id = featureId;
									feature.st = featureSt;
									feature.color = texture2D(model_batchTexture, featureSt);
								}
								else
								{
									feature.id = model_featuresLength + 1;
									feature.st = vec2(0.0);
									feature.color = vec4(1.0);
								}

								#ifdef HAS_NULL_FEATURE_ID
								if (featureId == model_nullFeatureId) {
									feature.id = featureId;
									feature.st = vec2(0.0);
									feature.color = vec4(1.0);
								}
								#endif
							}
							SelectedFeature selectedFeature;
							void main(){
                                vec4 position = czm_inverseModelView * vec4(v_positionEC,1); // 位置
                                float glowRange = 300.0; // 光环的移动范围(高度)
                                gl_FragColor = vec4(0.0, 0.3, 0.8, 0.8); // 颜色

                                // 小于20米的低楼都不再变暗
                                if(position.y > 20.0) {
                                    gl_FragColor *= vec4(vec3(position.y / 20.0), 0.8); // 渐变
                                }

                                // 动态光环
                                float time = fract(czm_frameNumber / 360.0);
                                time = abs(time - 0.5) * 3.0;
                                float diff = step(0.005, abs( clamp(position.y / glowRange, 0.0, 1.0) - time));
                                gl_FragColor.rgb += gl_FragColor.rgb * (1.0 - diff);
							}
						`;
            }
        }
    })

    const default_headingPitchRange = new Cesium.HeadingPitchRange(
        0.1,
        -0.5,
        16000
    );
    viewer.flyTo(tileset, { offset: default_headingPitchRange });

})

</script>